package test1;

public class Test3 {
    public static void main(String[] args) {
        int []arr={423,52345,523425,543,654,754,5,653,3463,63,6};
        B.mergeSort(arr,0,arr.length-1);
        B.print(arr);
    }
}


class B{
    public static void mergeSort(int[]arr,int left,int right){
        if(left==right){
            return;
        }

        int mid=(left+right)/2;
        mergeSort(arr,left,mid);
        mergeSort(arr,mid+1,right);
        merge(arr,left,right,mid);
    }

    public static void merge(int[]arr,int left,int right,int mid){
        int s1=left;
        int e1=mid;
        int s2=mid+1;
        int e2=right;

        int temarr[]=new int[right-left+1];
        int k=0;//记录临时数组下标
        while(s1<e1&&s2<e2){
            if(arr[s1]<=arr[s2]){
                temarr[k++]=arr[s1++];
            }
            else {
                temarr[k++]=arr[s2++];
            }
        }
        while(s1<e1){
            temarr[k++]=arr[s1++];
        }
        while(s2<e2){
            temarr[k++]=arr[s2++];
        }

        for(int i=0;i<temarr.length;i++){
            arr[i+left]=temarr[i];
        }
    }

    public static void print(int []arr){
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }

}
